package com.buddy.qreport.web.query.controller;

import com.buddy.qreport.common.mybatis.entity.Page;
import com.buddy.qreport.datasource.service.IDataSourceService;
import com.buddy.qreport.query.entity.QueryEntity;
import com.buddy.qreport.query.service.IQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController("QREPORT_QueryController")
@RequestMapping("/query")
public class QueryController {


    @Autowired
    @Qualifier("QREPORT_DataSourceService")
    private IDataSourceService dataSourceService;

    @Autowired
    @Qualifier("QREPORT_QueryService")
    private IQueryService queryService;

    /**
     * 测试查询参数的数据源sql
     * @return
     */
    @PostMapping("/testQueryDataSource")
    public ResponseEntity testQueryDataSource(@RequestParam("datasourceId") String datasourceId,@RequestParam("sql") String sql){



        Map result = new HashMap();



        boolean status = false;

        String message = null;

        do{


            if(!sql.trim().startsWith("select") || sql.contains("deleteReport") ||sql.contains("update")||sql.contains("insert") || sql.contains("drop") || sql.contains("alter") || sql.contains("create")){

                message = "sql格式错误";

                break;
            }

            if(! dataSourceService.avaiable(datasourceId)){

                message = "当前数据源不可用";

                break;
            }


            try {

               List<Map> testResult = queryService.testSql(datasourceId,sql);

               result.put("data",testResult);

               status = true;


            } catch (Exception e) {
                //e.printStackTrace();

                message = e.getLocalizedMessage();
            }


        }while (false);




        result.put("status",status);

        result.put("message",message);



        return new ResponseEntity(result, HttpStatus.OK);

    }


    @GetMapping("/list")
    public ResponseEntity list(Page page){

        Page<QueryEntity> queryEntityPage = queryService.selectList(page);

        return new ResponseEntity(queryEntityPage, HttpStatus.OK);

    }

    @GetMapping("/getQuery")
    public ResponseEntity list(@RequestParam("queryId") String queryId){

        QueryEntity queryEntity = queryService.getOne(queryId);

        return new ResponseEntity(queryEntity, HttpStatus.OK);

    }


    @GetMapping("/deleteQuery")
    public ResponseEntity deleteQuery(@RequestParam("queryId") String queryId){

        queryService.delete(queryId);

        return new ResponseEntity(HttpStatus.OK);
    }


    @PostMapping("/addQuery")
    public ResponseEntity addQuery(@RequestBody QueryEntity entity){




        Map result = new HashMap();



        boolean status = false;

        String message = null;


        do{


            if(StringUtils.isEmpty(entity.getQuerParamsConfig())){

                message = "参数错误";

                break;
            }

            if(StringUtils.isEmpty(entity.getQueryResultConfig())){

                message = "参数错误";

                break;
            }
            if(StringUtils.isEmpty(entity.getQueryDatasource())){

                message = "数据源为空";
                break;
            }

            if(StringUtils.isEmpty(entity.getQuerySql())){

                message = "sql为空";
                break;
            }



            if(! dataSourceService.avaiable(entity.getQueryDatasource())){

                message = "当前数据源不可用";

                break;
            }


            queryService.insert(entity);

            message = "保存成功";
            status = true;



        }while (false);



        result.put("status",status);

        result.put("message",message);



        return new ResponseEntity(result, HttpStatus.OK);

    }

}
